from rest_framework import serializers

from home.models import NavCategory, CourseCategory, Banner


class NavCategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = NavCategory
        fields = ["id", "name", "weight", "external_link"]


class CourseCategorySerializer(serializers.ModelSerializer):
    children = serializers.SerializerMethodField()

    def get_children(self, obj):
        children = CourseCategory.objects.filter(parent=obj)
        if children:
            ser = CourseCategorySerializer(children, many=True)
            return ser.data
        return []

    class Meta:
        model = CourseCategory
        fields = ["id", "name", "level", "parent", "desc", "children"]


class BannerSerializer(serializers.ModelSerializer):
    class Meta:
        model = Banner
        fields = ["id", "title", "image", "link", "note"]
